package com.ruoyi.config.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.util.Tools;
import com.ruoyi.config.entity.FileWordConfigEntity;
import com.ruoyi.config.service.IFileWordConfigService;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/system/oa/FileWord")
public class FileWordConfigController extends BaseController {

    @Autowired
    private IFileWordConfigService fileWordConfigService;

    @Autowired
    private TokenService tokenService;

    //新增修改
    @RequestMapping("/saveFileWord")
    public AjaxResult saveFileWord(HttpServletRequest request, @RequestBody FileWordConfigEntity fileWordConfigEntity) {
        System.out.println("--------------------------------------");
        System.out.println(fileWordConfigEntity.toString());
        System.out.println("--------------------------------------");
        fileWordConfigEntity.setModifiTime(Tools.getCurrentDateTime());
        LoginUser loginUser = tokenService.getLoginUser(request);
        fileWordConfigEntity.setUserCN(loginUser.getUsername());
        fileWordConfigEntity.setUserId(String.valueOf(loginUser.getUser().getUserId()));
        if (fileWordConfigEntity.getId() == null) {
            fileWordConfigEntity.setId(Tools.createUUID());
            return toAjax(fileWordConfigService.insert(fileWordConfigEntity));
        } else {
            return toAjax(fileWordConfigService.updateByPrimaryKey(fileWordConfigEntity));
        }
    }

    //列表展示
    @RequestMapping("/listFileWord")
    public TableDataInfo listFileWord() {
        startPage();
        List<FileWordConfigEntity> list = fileWordConfigService.selectAllAdmin();
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getConfigKey(@PathVariable String id) {
        return AjaxResult.success(fileWordConfigService.selectByPrimaryKey(id));
    }

    //状态修改
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody FileWordConfigEntity fileWordConfigEntity) {

        return AjaxResult.success(fileWordConfigService.updateByPrimaryKeySelective(fileWordConfigEntity));
    }

    //数据删除
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return AjaxResult.success(fileWordConfigService.deleteFileWordByIds(ids));
    }

    //查询
    @RequestMapping("/listFileWordForKey")
    public TableDataInfo listFileWordForKey(String queryInfo){
        startPage();
        List<FileWordConfigEntity> list = fileWordConfigService.selectQueryKey(queryInfo);
        return getDataTable(list);
    }
}
